<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
//收集表单数据（账号密码）
import { reactive, ref } from 'vue'
//引入用户相关的小仓库
import useUserStore from '@/store/modules/user';
//消息提醒框
import { ElNotification } from 'element-plus';
import { useRouter,useRoute } from 'vue-router';
//引入获取当前时间的函数
import { getTime } from '@/utils/time';


//获取el-form组件
let loginForms = ref()

let useStore = useUserStore();
//获取路由器
let $router = useRouter();
//获取路由对象
let $route = useRoute();
//定义变量控制按钮加载效果
let loading = ref(false)//起始值

//自定义规则
const validatorUsername = (rule:any,value:any,callback:any) => {
    if (value.length >= 5){
        callback()
    }else{
        callback(new Error('账号长度至少五位'))
    }
}

const validatorPassword = (rule:any,value:any,callback:any) => {
    if (value.length >= 6){
        callback()
    }else{
        callback(new Error('密码长度至六位'))
    }
}
//定义表单校验需要配置对象
const rules = {
    username: [
        { validator: validatorUsername, trigger: 'change' }
        // { required: true, min: 5, max: 10, message: '账号长度至少五位', trigger: 'change' }
    ],
    password: [
        { validator: validatorPassword, trigger: 'change' }
        // { required: true, min: 6, max: 10, message: '密码长度至六位', trigger: 'change' }
    ]
}

let loginForm = reactive({ username: 'admin', password: '111111' })
const login = async () => {
    //保证全部表单都校验通过再发请求
    await loginForms.value.validate();

    //开始加载
    loading.value = true;
    //点击登录按钮
    //通知仓库发登录请求
    //请求成功->首页展示
    //请求失败->弹出登陆失败信息
    try {//成功
        await useStore.userLogin(loginForm);
        //判断登陆时是否有query参数，有则往query跳，没有则跳首页
        let redirect:any = $route.query.redirect;
        $router.push({path:redirect||'/'})
        //提示信息
        ElNotification({
            type: 'success',
            message: '欢迎回来',
            title: `HI,${getTime()}好`
        })
        loading.value = false;
    } catch (error) {
        //登陆失败加载效果
        loading.value = false;
        //失败
        ElNotification({
            type: 'error',
            message: (error as Error).message
        })

    }
}

</script>

<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0">zuo</el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password"
                            show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="default" class="login_btn" :loading="loading"
                            @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>

</template>

<style scoped lang="scss">
.login_container {
    width: 100%;
    height: 100vh;
    background: url("@/assets/images/background.jpg");
    background-size: cover;
}

.login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
        color: white;
        font-size: 40px;
    }

    h2 {
        color: white;
        font-size: 20px;
        margin: 20px 0px;
    }

    .login_btn {
        width: 100%;
    }
}
</style>
